<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 友情链接控制器类
 * @author wwj
 * @version 1.0.1 2012-12-11
 */
class Friend_link extends MY_Controller
{
	function __construct()
	{
		parent::__construct();

		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_FRIEND_LINK);
		
		$this->load->model('friend_link_m');

		// 加载友情链接的语言包
		$this->load->language('friend_link');
		
		$this->validate_data = array(
				array('field' => 'website', 'label' => 'lang:link_website', 'rules' => 'trim|required|max_length[20]|xss_clean'),
				array('field' => 'linkpath', 'label' => 'lang:link_linkpath', 'rules' => 'trim|required'),
				array('field' => 'sort', 'label' => 'lang:link_sort', 'rules' => 'trim|required|is_natural'),
				array('field' => 'isactive', 'label' => 'lang:link_isactive', 'rules' => 'trim|required|is_natural'),
				array('field' => 'remark', 'label' => 'lang:link_remark', 'rules' => 'trim|max_length[50]'),
				);
	}
	
	/**
	 * 友情链接的默认方法，显示友情链接列表
	 * @param int $page 要显示的列表当前页
	 */
	function index($page = '')
	{
		$this->link_list($page);
	}
	
	/**
	 * 显示友情链接列表
	 * @param int $page 要显示的列表当前页
	 */
	function link_list($page = '')
	{
		// 获取要显示的页数
		$page = empty($page) ? 1 : abs(intval($page));
		
		// 组织分页信息
		$config['base_url']		= base_url('friend_link/index');
		$config['total_rows']	= $this->friend_link_m->get_link_count();
		
		$page_data = $this->get_page($config);
		$data['page'] 	= $page_data['page'];

		// 查询友情链接
		$links      = $this->friend_link_m->show_link($page_data['limit']);
		
		// 友情链接信息列表数组
		$i = 1;
		foreach ($links as $key => $row)
		{
			$links[$key]['isactive'] = $row['isactive'] == 1 ? $this->lang->line('com_sign_active') : $this->lang->line('com_sign_inactive');
			$links[$key]['ordernum'] = ($page - 1) * $page_data['per_page'] + $i;
			$links[$key]['createtime'] = date(DATE_FORMAT,$row['createtime']);
			$i++;
		}
		
		$data['header'] 		= $this->header;
		$data['link_list']		= $links;
		$data['title']			= $this->lang->line('link_title_list');
		$data['page_label']		= $this->lang->line('link_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$this->load->view('friend_link_list',$data);
	}
	
	/**
	 * 添加友情链接的显示页面
	 */
	function add()
	{
		$data = array (
			
				'header' => $this->header,
				'title' => $this->lang->line('link_title_add'),
				'page_label' => $this->lang->line('link_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label') 
		);
		$this->load->view('friend_link_add', $data);
	}

	/**
	 * 处理添加友情链接业务流程，返回操作提示信息
	 */
	function add_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('friend_link/add'), $validate, 'goback');
		}
		$link = $this->input->post();
		if ($link && isset($link['s_add_action']))
		{
			// 清除数组中无用的数据，添加相关信息
			unset($link['s_add_action']);
			$link['linktype'] 	= '文字';
			$link['createtime'] = time();

			// 添加友情链接信息
			$result = $this->friend_link_m->insert_link($link);
			if ($result != FALSE)
			{
				$this->_show_message(base_url('friend_link'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('friend_link/add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('friend_link'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 编辑友情链接的显示页面
	 * @param int $id 友情链接ID
	 */
	function edit($id)
	{
		$link_id['id'] = intval($id);
		if (empty($link_id))
		{
			$this->_show_message(base_url('friend_link'), $this->lang->line('link_id_unable_empty'), 'goback');
		}
		
		// 查询友情链接信息
		$link = $this->friend_link_m->get_link($link_id);
		if (empty($link))
		{
			$this->_show_message(base_url('friend_link'), $this->lang->line('link_does_not_exist'), 'goback');
		}
		
		$data = array (
			
				'header' => $this->header,
				'link' => $link,
				'title' => $this->lang->line('link_title_edit'),
				'page_label' => $this->lang->line('link_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label') 
		);
		$this->load->view('friend_link_edit', $data);
	}
	
	/**
	 * 处理编辑友情链接业务
	 */
	function edit_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('friend_link/add'), $validate, 'goback');
		}
		
		// 获取表单数据并更新
		$link = $this->input->post();
		if ($link && isset($link['s_edit_action']))
		{
			$link_id['id'] = intval($link['id']);
			unset($link['s_edit_action']);
			unset($link['id']);
			$result = $this->friend_link_m->update_link($link_id, $link);
			$this->_show_message(base_url('friend_link'), $this->lang->line('msg_operation_success'));
		}
	}
	
	/**
	 * AJAX方式删除友情链接，接收ID数组，返回JSON格式的数据
	 * 
	 * JSON数据包含error是否成功的标志，和返回的数据信息message
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:link_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
		
		// 获取友情链接ID值，并放入数组
		$link_id['id'] = $this->input->post('id');
		if ($this->friend_link_m->delete_link($link_id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
}

/* End of file friend_link.php */
/* Location: ./application/admin/controllers/friend_link.php */